package com.jianshuge.app.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jianshuge.app.AppException;
import com.jianshuge.app.bean.MyBooklistDetail.MyBooklistItem;
import com.jianshuge.app.bean.MyBooklistDetail.Reply;
import com.jianshuge.app.common.StringUtils;

import android.os.Parcel;
import android.os.Parcelable;

public class MyBookDetail extends Entity {
	
	private String  id;
	private String  isbn; 
	private String  title;
	private String  author;
	private String  pubdate;
	private String  publisher;
	private String  summary ;
	private String  keywords; 
	private String  image_url; 
	private String  subject_id;
	private String  rating; 
	private boolean favorite_status;
	
	private List<Reply> replies = new ArrayList<Reply>();
	private List<doubanComment> doubanComments = new ArrayList<doubanComment>();
	

	public static class Reply implements Serializable{
		public String rauthor;
		public String rpubDate;
		public String rcontent;
	}
	
	public static class doubanComment implements Serializable{
		public String title;
		public String summary;
		public String author;
		public String author_uri;
		public String author_image;
		public String comment_uri;
		public String rating;
	};
	
	
	public List<doubanComment> getDoubanComments() {
		return doubanComments;
	}
	public boolean getFavoriteStatus() {
		return favorite_status;
	}
	
	public void setFavoriteStatus(boolean status) {
		this.favorite_status = status;
	}
	public String getIsbn( ) {
		return isbn;
	}
	
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}
	
	public void setAuthor(String author) {
		this.author = author;
	}
	
	public String getPubdate() {
		return pubdate;
	}
	
	public void setPubdate(String pubdate) {
		this.pubdate = pubdate;
	}
	
	public String getPublisher() {
		return publisher;
	}
	
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	
	public String getSummary() {
		return summary;
	}
	
	public void setSummary(String summary) {
		this.summary = summary;
	}
	
	public String getkeywords() {
		return keywords;
	}
	
	public void setkeywords(String keywords) {
		this.keywords = keywords;
	}
	
	public String getImageUrl() {
		return image_url;
	}
	public void setImageUrl(String image_url) {
		this.image_url = image_url;
	}
	
	public String getSubjectId() {
		return subject_id;
	}
	
	public void setSubjectId(String subject_id) {
		this.subject_id = subject_id;
	}

	public String getRating() {
		return rating;
	}
	
	public void setRating(String rating) {
		this.rating = rating;
	}
	
	public static MyBookDetail parse(InputStream inputStream) throws IOException, AppException {
		
		MyBookDetail my_book_detail = new MyBookDetail();

		String inputStr;
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream) ); 
	    StringBuilder responseStrBuilder = new StringBuilder();
	    while ((inputStr = streamReader.readLine()) != null)
	        responseStrBuilder.append(inputStr);
	    String strResult = responseStrBuilder.toString();
	    
		try {
			
			JSONObject doubanbook_json_obj = new JSONObject(strResult);
			my_book_detail.id = doubanbook_json_obj.optString("id");
			my_book_detail.setTitle(doubanbook_json_obj.optString("title"));
			my_book_detail.setAuthor(doubanbook_json_obj.optString("author"));
			my_book_detail.setPublisher(doubanbook_json_obj.getString("publisher"));
			my_book_detail.setPubdate(doubanbook_json_obj.getString("pubdate"));
			my_book_detail.setImageUrl(doubanbook_json_obj.optString("image_url_m"));
			my_book_detail.setIsbn(doubanbook_json_obj.optString("isbn"));
			my_book_detail.setRating( doubanbook_json_obj.optString("rating") );
			my_book_detail.setSummary(doubanbook_json_obj.optString("summary"));
			my_book_detail.setkeywords(doubanbook_json_obj.optString("keywords"));
			my_book_detail.setFavoriteStatus(doubanbook_json_obj.getBoolean("collection_status"));			
			my_book_detail.setSubjectId(doubanbook_json_obj.optString("subject_id"));	

			JSONArray reply_json_arr = doubanbook_json_obj.optJSONArray("comments");
			if(reply_json_arr != null) {
				for (int j = 0; j < reply_json_arr.length(); j++) {
					JSONObject comment_json_obj = reply_json_arr.getJSONObject(j);
					doubanComment comment = new doubanComment();
					comment.title 			= comment_json_obj.optString("title");
					comment.summary 		= comment_json_obj.optString("summary");
					comment.author 			= comment_json_obj.optString("author");
					comment.author_uri 		= comment_json_obj.optString("author_uri");
					comment.author_image	= comment_json_obj.optString("author_image");
					comment.comment_uri 	= comment_json_obj.optString("com_uri");
					comment.rating		 	= comment_json_obj.optString("rating");
					my_book_detail.doubanComments.add(comment);
				}
			}
			
		} catch (JSONException e) { 
            System.out.println("Jsons parse error !"); 
            e.printStackTrace(); 
        }
		
	    return my_book_detail;
	}

}
